package com.gz.common.mongodb.base;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;

import java.util.Date;

/**
 * @ClassName MongoBaseModel
 * @Description //
 * @Author WHX
 * @Date 2021/8/15 22:49
 **/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class MongoBaseModel<T> {
    /**
     * id
     */
    @Id
    private T id;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 修改时间
     */
    private Date updateTime;

    /**
     * 是否删除：0未删除、1删除
     */
    private Boolean del;

    public MongoBaseModel<T> baseDataAsNull() {
        this.id=null;
        this.createTime=null;
        this.updateTime=null;
        this.del=null;
        return this;
    }

    /**
     * 插入数据时初始化公共默认字段
     * @param <Model>
     * @return
     */
    public <Model> Model init() {
        this.id=null;
        this.createTime=new Date();
        this.updateTime=new Date();
        this.del=false;
        return (Model)this;
    }

    public String toJsonString(){
        return JSONObject.toJSONString(this, SerializerFeature.WriteMapNullValue);
    }

}
